﻿package com.mdp.model 
{
	
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.events.NetStatusEvent;
	import com.mdp.model.User;
	import flash.net.GroupSpecifier;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Stream 
	{
	
		private const SERVER:String = "rtmfp://p2p.rtmfp.net/";
		private const DEVKEY:String = "4c3b214f5631b2a09de23c2e-f8743d8bdce5";
		
		public var ns:NetStream;
		public var nc:NetConnection;
		public var cam:Camera;
		public var mic:Microphone;
		
		public var streams:Array;
		protected var streamsInProgress:Array;
	
		public function Stream() 
		{
			streams = new Array();
			streamsInProgress =  new Array();
			//flash.system.Security.showSettings(flash.system.SecurityPanel.CAMERA);
			//VIDEO STREAMING STUFF
			// create basic netConnection object
			nc = new NetConnection();
			nc.client = this;
			// connect to the local Red5 server
			nc.connect(SERVER+DEVKEY);
			nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
		}
		
		public function onBWDone():void {

		}
		
		public function subscribeSteam(s:String):NetStream {
			
			var groupspec:GroupSpecifier = new GroupSpecifier("mdp/mdp");
			groupspec.serverChannelEnabled = true;
			groupspec.multicastEnabled = true;
			
			var newStream:NetStream = new NetStream(nc,groupspec.groupspecWithAuthorizations());
			newStream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
			
			streamsInProgress.push(s);
			streams[s] = newStream;
			
			return newStream;
		}
		
		  public function netStatus(e:NetStatusEvent):void {
			 switch (e.info.code) {
				case "NetStatus.Connect.Success":
					
					//pop strem from array
					var s:String = streamsInProgress.shift();
					streams[s].videoSampleAccess = true;
					streams[s].play("1");
				
					break;
				case "NetStream.Play.Start":
					e.target.videoSampleAccess =  true;	
					ns.videoSampleAccess = true;
					ns.send("|RtmpSampleAccess", true, true);
					break;
				case "NetGroup.Neighbor.Connect":
					e.target.videoSampleAccess =  true;	
					ns.videoSampleAccess = true;
					ns.send("|RtmpSampleAccess", true, true);
					break;
				default :
					//Logger.log("unknown netStream status=" + e.info.code);
			break;
			}
	   }
		
	   public function myNetStatus(e:NetStatusEvent):void {
		    switch (e.info.code) {
				case "NetStatus.Connect.Success":
					
					trace("myNet success");
				
					ns.videoSampleAccess = true;
					ns.send("|RtmpSampleAccess", true, true);
				
					// setup some simple settings with the cam and mic for quality
					if(setupCameraMic()){
						ns.attachCamera(cam);
						ns.attachAudio(mic);
						
						// publish your cam/mic to red5 for broadcasting
						ns.publish(User.id, "live");
					} 
					
				break;
				case "NetStream.Play.Start":
					trace("myNetP Play start");
					e.target.videoSampleAccess =  true;	
					ns.videoSampleAccess = true;
					ns.send("|RtmpSampleAccess", true, true);
					break;
				case "NetGroup.Neighbor.Connect":
					trace("My net neighbor connect");
					e.target.videoSampleAccess =  true;	
					ns.videoSampleAccess = true;
					ns.send("|RtmpSampleAccess", true, true);
					break;
				default :
					//Logger.log("unknown netStream status=" + e.info.code);
			break;
			}
	   }
	   
		public function netStatusHandler( e:NetStatusEvent ):void {
		   switch (e.info.code) {
                case "NetConnection.Connect.Success":
					
					trace("netStatus connect success");
				
					var groupspec:GroupSpecifier = new GroupSpecifier("mdp/mdp");
					groupspec.serverChannelEnabled = true;
					groupspec.multicastEnabled = true;
					
					ns = new NetStream(nc,groupspec.groupspecWithAuthorizations());
					ns.addEventListener(NetStatusEvent.NET_STATUS, myNetStatus);
					
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Stream not found: ");
                    break;
				default:
					trace( e.info.code );
					break;
            }
		  
	   }
		
		public function setupCameraMic():Boolean
		{
			try {
				// setup cam
				cam = Camera.getCamera();
				// setting dimensions and framerate
				cam.setMode(320, 240, 30);
				// set to minimum of 70% quality
				cam.setQuality(0,70);
				// setup mic
				mic = Microphone.getMicrophone();
				
				return true;
			} catch (e:Error) {
				return false;
			}
			
			return false;
		}
		
	}
	
}